﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using web.data.entities;
using web.bussines;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using web.component;

namespace web.core.Models
{
    public class siteSettingModels
    {
        //get list language, setting_type =-1: get all
        public static PagedList<site_settings> get_plist(int pageindex, int pagesize)//, int setting_type)
        {
            var obj = new site_setting_service();
            return obj.adm_get_plist(pageindex, pagesize); //, setting_type);
        }

        //get list language, to create setting file config
        public static Dictionary<string, string> get_list_config(int langid)
        {
            var obj = new site_setting_service();
            var dict = new Dictionary<string, string>();
            var lst = obj.get_list_config(langid);
            foreach(var it in lst){
                dict.Add(it.setting_name, it.setting_content);
            }
            return dict;
        }

        public static int adm_setting_update(site_settings item)
        {
            var obj = new site_setting_service();
            return obj.adm_setting_update(item);
        }

        //get items by id or code
        public static site_settings getByID(string setting_name, int? language_id)
        {
            var obj = new site_setting_service();

            return obj.getByID(setting_name, language_id ?? admin_const.DEFAULT_LANGUAGE_ID);
        }

        //get items by id or code
        public static string getValueByID(string setting_name, int? language_id)
        {
            string strName = getByID(setting_name, language_id).setting_content;
            return strName;
        }

        public static void deleteSetting(string setting_name, int? language_id)
        {
            var obj = new site_setting_service();
            obj.deleteSetting(setting_name, language_id?? admin_const.DEFAULT_LANGUAGE_ID);
        }


    }
    //single model for instance
    public class siteSettingSingleModels
    {
        private static object _SyncRoot = new object();
        private static Dictionary<string, string> _dicSetting;

        public static Dictionary<string, string> dicSetting
        {
            get
            {
                if (_dicSetting == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_dicSetting == null)
                        {
                            _dicSetting = get_common_config();
                        }
                    }
                }
                return _dicSetting;
            }
        }

        //get list language, to create setting file config
        public static Dictionary<string, string> get_common_config()
        {
            var obj = new site_setting_service();
            var dict = new Dictionary<string, string>();
            //nhung truong duoi day thuong xuyen dc su dung lai
            dict.Add(global_const.STORAGE_CONFIG, obj.getByID(global_const.STORAGE_CONFIG, admin_const.NO_LANGUAGE).setting_content);
            dict.Add(global_const.WEBSITE_URL, obj.getByID(global_const.WEBSITE_URL, admin_const.NO_LANGUAGE).setting_content);

            //email settings
            dict.Add(global_const.SMTPPASS, obj.getByID(global_const.SMTPPASS, admin_const.NO_LANGUAGE).setting_content);
            dict.Add(global_const.SMTPPORT, obj.getByID(global_const.SMTPPORT, admin_const.NO_LANGUAGE).setting_content);
            dict.Add(global_const.SMTPUSER, obj.getByID(global_const.SMTPUSER, admin_const.NO_LANGUAGE).setting_content);
            dict.Add(global_const.SMTPHOST, obj.getByID(global_const.SMTPHOST, admin_const.NO_LANGUAGE).setting_content);

            return dict;
        }

        public static string GetConfig(string strkey)
        {
            if (dicSetting.ContainsKey(strkey))
            {
                return dicSetting[strkey].Trim();
            }
            else
            {
                return string.Empty;
            }
        }

        public static void UpdateConfig(string strkey, string strvalue)
        {
             if (dicSetting.ContainsKey(strkey))
            {
                dicSetting[strkey] = strvalue;
            }
        }
    }
}